﻿using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Common.Framework.Core.Common
{
    /// <summary>
    /// 服务接口统一响应
    /// </summary>
    public class ServiceResult : ServiceResult<object>
    {

    }

    /// <summary>
    /// 服务接口统一响应看
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ServiceResult<T>
    {
        /// <summary>
        /// 状态码
        /// </summary>
        public int Code { get; set; } = StatusCodes.Status200OK;

        /// <summary>
        /// 数据
        /// </summary>
        public T Data { get; set; }

        /// <summary>
        /// 消息
        /// </summary>
        public string Msg { get; set; } = "";

        /// <summary>
        /// 服务器时间戳
        /// </summary>
        public long Timestamp => DateTimeOffset.Now.ToUnixTimeMilliseconds();

        /// <summary>
        /// 服务来源
        /// </summary>
        public string Source => Environment.MachineName;

        /// <summary>
        /// 响应成功
        /// </summary>
        /// <param name="output">响应数据</param>
        /// <returns></returns>
        public static ServiceResult<T> Success(T output)
        {
            return new ServiceResult<T> { Data = output };
        }

        /// <summary>
        /// 响应成功
        /// </summary>
        /// <param name="msg">成功消息</param>
        /// <returns></returns>
        public static ServiceResult<T> Success(string msg)
        {
            return new ServiceResult<T> { Msg = msg };
        }

        /// <summary>
        /// 响应成功
        /// </summary>
        /// <param name="output"></param>
        /// <param name="msg">成功消息</param>
        /// <returns></returns>
        public static ServiceResult<T> Success(T output, string msg)
        {
            return new ServiceResult<T> { Data = output, Msg = msg };
        }

        /// <summary>
        /// 响应失败
        /// </summary>
        /// <param name="code">错误状态码</param>
        /// <param name="msg">错误消息</param>
        /// <returns></returns>
        public static ServiceResult<T> Faild(int code, string msg)
        {
            return new ServiceResult<T> { Code = code, Msg = msg };
        }
    }
}
